package org.s170.dao;



import org.s170.entity.Admintrao;
import org.s170.utils.Dbutil;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AdmintratoDao {
    public Admintrao queyUser(String account, String pass) {
        Admintrao admintrato = null;
        String sql = "select * from admintrato where account=? and pass=?";
            Connection connection = Dbutil.getConnection();
        PreparedStatement stmt = null;
        ResultSet rst = null;
        try {
            stmt = connection.prepareStatement(sql);
            stmt.setString(1, account);
            stmt.setString(2, pass);
            rst = stmt.executeQuery();
            if (rst.next()) {
                admintrato = new Admintrao();
                admintrato.setAdminID(rst.getString(1));
                admintrato.setAccout(rst.getString(2));
                admintrato.setPass(rst.getString(3));
                admintrato.setImages(rst.getString(4));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            Dbutil.closeAll(connection, stmt, rst);
        }
        return admintrato;
    }
    public List<Admintrao> queryAll(){
        List<Admintrao> list=new ArrayList<>();
        String sql="select id,account,pass,images from admintrato";
        Connection connection=Dbutil.getConnection();
        PreparedStatement stmt=null;
        ResultSet rst=null;
        try{
            stmt=connection.prepareStatement(sql);
            rst=stmt.executeQuery();
            while(rst.next()){
             Admintrao   admintrato = new Admintrao();
                admintrato.setAdminID(rst.getString(1));
                admintrato.setAccout(rst.getString(2));
                admintrato.setPass(rst.getString(3));
                admintrato.setImages(rst.getString(4));
                list.add(admintrato);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            Dbutil.closeAll(connection,stmt,rst);
        }
        return list;
    }
    public int delete(String adminID){
        int r=0;
        String sql="delete from admintrato where id=?";
        Connection connection=Dbutil.getConnection();
        PreparedStatement stmt=null;
        try{
            stmt=connection.prepareStatement(sql);
            stmt.setString(1,adminID);
            r=stmt.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            Dbutil.closeAll(connection,stmt,null);
        }
        return r;
    }

}
